package com.mutouren.common.utils;

import java.security.MessageDigest;

public class DigestUtils {
	
	public static String MD5(String source) {
		return MD5(source, "utf-8");
	}
	
	public static String MD5(String source, String charsetName) {
		return doDigest("MD5", source, charsetName);
	}
	
	public static String SHA(String source) {
		return SHA(source, "utf-8");
	}
	
	public static String SHA(String source, String charsetName) {
		return doDigest("SHA", source, charsetName);
	}	
	
	public static String SHA256(String source) {
		return SHA256(source, "utf-8");
	}
	
	public static String SHA256(String source, String charsetName) {
		return doDigest("SHA-256", source, charsetName);
	}
	
	public static String SHA512(String source) {
		return SHA512(source, "utf-8");
	}
	
	public static String SHA512(String source, String charsetName) {
		return doDigest("SHA-512", source, charsetName);
	}		
	
    private static String doDigest(String algorithm, String source, String charsetName) {
		try 
	    {
	        MessageDigest md5 = MessageDigest.getInstance(algorithm);
	        byte[] bytes = md5.digest(source.getBytes(charsetName));
	        return StringUtils.byteArr2HexStr(bytes);
	    } 
	    catch (Exception e) 
	    {
            throw new RuntimeException(e.getMessage());
	    }
    }	

	public static void main(String[] args) {
		System.out.println(MD5("ssssssssssssssss"));
		System.out.println(SHA("ssssssssssssssss"));		
		System.out.println(SHA256("ssssssssssssssss"));
		System.out.println(SHA512("ssssssssssssssss"));

	}

}
